/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.server;

import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.orkut.client.api.Album;
import com.google.orkut.client.api.AlbumsTxFactory;
import com.google.orkut.client.api.GetAlbumsTx;
import com.google.orkut.client.api.GetPhotosTx;
import com.google.orkut.client.api.Photo;
import com.google.orkut.client.api.PhotosTxFactory;
import com.google.orkut.client.api.samples.greetings.client.services.AlbumInfo;
import com.google.orkut.client.api.samples.greetings.client.services.AlbumsService;
import com.google.orkut.client.api.samples.greetings.client.services.PhotoInfo;
import com.google.orkut.client.api.samples.greetings.server.AuthorizedChannel.Factory;
import com.google.orkut.client.api.samples.greetings.shared.AuthorizationException;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;

/**
 * Implements {@link AlbumsService} using orkut client library.
 *
 * @author Shishir Birmiwal
 */
@Singleton
public class AlbumServiceImpl extends RemoteServiceServlet implements AlbumsService {
  private static final int MAX_PHOTOS = 100;
  private static final int MAX_ALBUMS = 100;
  private final Factory factory;
  private final AlbumsTxFactory albumTxFactory;
  private final PhotosTxFactory photoTxFactory;
  private final CachedConfig cachedConfig;

  @Inject
  public AlbumServiceImpl(
      AlbumsTxFactory albumTxFactory,
      AuthorizedChannel.Factory factory,
      CachedConfig cachedConfig,
      PhotosTxFactory photoTxFactory) {
    this.factory = factory;
    this.albumTxFactory = albumTxFactory;
    this.cachedConfig = cachedConfig;
    this.photoTxFactory = photoTxFactory;
  }

  @Override
  public ArrayList<AlbumInfo> getAlbumsOf(String postToken, String userId)
  throws AuthorizationException {
    AuthorizedChannel authorizedChannel = factory.create(postToken, getRequest());
    GetAlbumsTx getAlbumsTx = albumTxFactory.getAlbums(userId);
    getAlbumsTx.setCount(MAX_ALBUMS);
    try {
      authorizedChannel.add(getAlbumsTx).run();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    ArrayList<AlbumInfo> albums = Lists.newArrayList();
    for (int i = 0; i < getAlbumsTx.getAlbumCount(); i++) {
      Album album = getAlbumsTx.getAlbum(i);
      albums.add(convertToAlbumInfo(album));
    }
    return albums;
  }

  private HttpServletRequest getRequest() {
    return getThreadLocalRequest();
  }

  private AlbumInfo convertToAlbumInfo(Album album) {
    AlbumInfo info = new AlbumInfo();
    info.id = album.getId();
    info.ownerId = album.getOwnerId();
    info.thumbnailUrl = album.getThumbnailUrl();
    info.title = album.getTitle();
    info.numPhotos = album.getNumPhotos();
    return info;
  }

  @Override
  public ArrayList<PhotoInfo> getPhotosOf(String postToken, String userId, String albumId)
  throws AuthorizationException {
    AuthorizedChannel authorizedChannel = factory.create(postToken, getRequest());
    GetPhotosTx getPhotosTx = photoTxFactory.getPhotos(userId, albumId);
    getPhotosTx.setCount(MAX_PHOTOS);

    try {
      authorizedChannel.add(getPhotosTx).run();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    ArrayList<PhotoInfo> photos = Lists.newArrayList();
    for (int i = 0; i < getPhotosTx.getPhotoCount(); i++) {
      photos.add(convertToPhotoInfo(getPhotosTx.getPhoto(i)));
    }
    return photos;
  }

  private PhotoInfo convertToPhotoInfo(Photo photo) {
    PhotoInfo info = new PhotoInfo();
    info.id = photo.getId();
    info.albumId = photo.getAlbumId();
    info.imageUrl = photo.getUrl();
    info.ownerId = photo.getOwnerId();
    info.thumbnailUrl = photo.getThumbnailUrl();
    info.title = photo.getTitle();
    return info ;
  }

  @Override
  public ArrayList<AlbumInfo> getSharedAlbums(String postToken) throws AuthorizationException {
    String albumUserId = cachedConfig.getConfig().getSharedAlbumUserId();
    if (albumUserId == null || albumUserId.trim().length() == 0) {
      return Lists.<AlbumInfo>newArrayList();
    }
    return getAlbumsOf(postToken, albumUserId);
  }
}
